<template>
    <Layout>
        <Form ref="form1" :model="form1" :label-width="120">
            <Row class="query-form" style="padding-top: 15px">
                <Col span="8">
                    <FormItem label="厂商：" prop="supplierName">
                        <Input class="query-form-el" type="text" v-model="form1.supplierName"></Input>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="经营范围：" prop="busiScope">
                        <Input class="query-form-el" type="text" v-model="form1.busiScope"></Input>
                    </FormItem>
                </Col>
                <Col span="8">
                    <Button @click="reset" class="ivu-btn-reset">重置</Button>
                    <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
                </Col>
            </Row>
            <Row class="query-form">
                <Col span="24">
                    <Button size="large" type="success" class="ivu-btn-add" icon="md-add" @click="addInfo">新增</Button>
                </Col>
            </Row>
        </Form>

        <Table ref='table' :height="tableHeight" size="small" :loading="loading" no-data-text="没有查询到相关数据!"
               @on-selection-change='getSelectRows'
               border :columns="columns1" :data="dataTable">
        </Table>


        <Row style="margin-right:10px;">
            <Col span="24">
                <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize"
                      @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray"
                      show-total show-sizer show-elevator/>
            </Col>
        </Row>

        <Modal title="供应商台账" :transfer=false v-model="model1" :width=600>
            <Row>
                <Col span="24">
                    <Form ref="form1datacopy" :model="form1datacopy" :rules="ruleCustom" label-position="top">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label="厂商：" prop="supplierName">
                                    <Input type="text" v-model="form1datacopy.supplierName"></Input>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label="地址：" prop="address">
                                    <Input type="text" v-model="form1datacopy.address"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label="联系人：" prop="linkman">
                                    <Input type="text" v-model="form1datacopy.linkman"></Input>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label="联系电话：" prop="telephone">
                                    <Input type="text" v-model="form1datacopy.telephone"></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="经营范围：" prop="busiScope">
                                    <Input v-model="form1datacopy.busiScope" type="textarea" :rows="3"/>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="备注：" prop="notes">
                                    <Input v-model="form1datacopy.notes" type="textarea" :rows="3"/>
                                </FormItem>
                            </Col>
                        </Row>
                        <!-- <Row type="flex" justify="space-around" class="code-row-bg">
                             <Col span="23" >
                                 <FormItem>
                                     <Button type="primary"  class='ivu-btn-save' :loading="buttonLoading" @click="submit('form1datacopy')" >确定</Button>
                                     <Button type="primary"  class='ivu-btn-cancel' @click="handleCancel"  >取消</Button>
                                 </FormItem>
                             </Col>
                         </Row>-->
                    </Form>
                </Col>
            </Row>
            <div slot="footer">
                <Button type="primary" class='ivu-btn-save' :loading="buttonLoading" @click="submit('form1datacopy')">
                    确定
                </Button>
                <Button type="primary" class='ivu-btn-cancel' @click="handleCancel">取消</Button>
            </div>
        </Modal>
        <Modal title="供应商台账" class="modal-view-detail" :transfer=false v-model="model2" :width=800>
            <Row>
                <Col span="24">
                    <Form :model="showdata" :label-width="160">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label="厂商：" prop="supplierName">
                                    {{showdata.supplierName}}
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label="地址：" prop="address">
                                    {{showdata.address}}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label="联系人：" prop="linkman">
                                    {{showdata.linkman}}
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label="联系电话：" prop="telephone">
                                    {{showdata.telephone}}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="经营范围：" prop="busiScope">
                                    {{showdata.busiScope}}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="备注：" prop="notes">
                                    {{showdata.notes}}
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </Col>
            </Row>
            <div slot="footer">
                <Button class='ivu-btn-save' @click="model2=false">关闭</Button>
            </div>
        </Modal>
    </layout>
</template>

<script>
    import {Api} from '../../../../api/index.js'
    import {sessionStorage} from '../../../../common/cacheUtils.js'

    export default {
        data() {
            return {
                form1: {
                    supplierName: '',
                    busiScope: ''
                },
                //分页参数
                pageSizeArray: [10, 20, 50],
                pagedata: {
                    total: 0,
                    pageSize: 10,
                    currentPage: 1,
                },
                columns1: [
                    {
                        title: "序号",
                        width: 80,
                        align: "center",
                        fixed: 'left',
                        render: (h, params) => {
                            return h('span', params.index + (this.pagedata.currentPage - 1) * this.pagedata.pageSize + 1);
                        }
                    },
                    {
                        title: '厂商',
                        align: 'center',
                        key: 'supplierName',
                    },
                    {
                        title: '地址',
                        key: 'address',
                        align: 'center'
                    },
                    {
                        title: '联系人',
                        key: 'linkman',
                        align: 'center'
                    },
                    {
                        title: '联系电话',
                        key: 'telephone',
                        align: 'center'
                    },
                    {
                        title: '经营范围',
                        key: 'busiScope',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 200,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px'
                                    },
                                    on: {
                                        click: () => {
                                            this.showData(params, 1);
                                        }
                                    }
                                }, '详情'),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                    },
                                    on: {
                                        click: () => {
                                            this.showData(params, 2);
                                        }
                                    }
                                }, '修改'),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        color: 'red'
                                    },
                                    on: {
                                        click: () => {
                                            this.deleteData(params);
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                ruleCustom: {
                    supplierName: [
                        {required: true, message: '  ', trigger: 'blur'}
                    ],
                    address: [
                        {required: true, message: '  ', trigger: 'blur'}
                    ],
                    linkman: [
                        {required: true, message: '  ', trigger: 'blur'}
                    ],
                    telephone: [
                        {required: true, message: '  ', trigger: 'blur'}
                    ]
                },
                form1datacopy: {},
                dataTable: [],
                showdata: {},
                model1: false,
                buttonLoading: false, model2: false,
                add: false,
                tableHeight: 10, loading: false,
            }
        },
        mounted: function () {
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 145;
            this.query();
        },
        methods: {
            pageChange(pageNum) {
                this.pagedata.currentPage = pageNum;
                this.query();
            },
            pageSizeChange(pageSize) {
                this.pagedata.pageSize = pageSize;
                this.query();
            },

            copyArray(arr) {
                return JSON.parse(JSON.stringify(arr));
            },

            showData(data, flag) {

                if (flag == 1) {
                    //查看页面
                    this.showdata = data.row;
                    this.model2 = true;
                } else {
                    //修改页面
                    this.add = false;
                    this.form1datacopy = data.row;
                    this.model1 = true;
                }
            },

            getSelectRows(rows) {
                this.deleteRows = rows;
            },

            deleteData(params) {
                let url = Api.deleteFireSupplier;
                this.$http.delete(url + '/' + params.row.supplierId, {params: {}}).then((res) => {
                    this.$Message.success('成功');
                    this.query()
                }).catch(e => {
                    this.$Message.error('请求失败' + e);
                })
            },

            query() {
                this.loading = true;
                let data = {
                    startPage: this.pagedata.currentPage,
                    size: this.pagedata.pageSize,
                    orgId: sessionStorage('userId').getObject('userId').orgInfo.parentId,
                }
                data = Object.assign(data, JSON.parse(JSON.stringify(this.form1)));

                let url = Api.queryFireSupplier;
                this.$http.post(url, data).then((res) => {
                    if (res.data.status == 200) {
                        this.dataTable = res.data.data.data;
                        console.log(this.dataTable)
                        this.pagedata.total = res.data.data.totalSize;
                        this.loading = false;
                    } else {
                        this.$Message.error("数据获取失败")
                    }
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            reset() {
                this.form1 = {}
            },
            handleCancel() {
                this.form1datacopy = {};
                this.model1 = false;
            },
            submit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.buttonLoading = true
                        let userId = sessionStorage('userId').getObject('userId').userId;
                        let orgId = sessionStorage('userId').getObject('userId').orgInfo.orgId;
                        if (this.add) {
                            this.form1datacopy.createUserId = userId;
                            this.form1datacopy.createOrgId = orgId;
                            this.$http.post(Api.saveFireSupplier, this.form1datacopy).then((res) => {
                                this.$Message.success("保存成功")
                                this.model1 = false;
                                this.query();
                                this.buttonLoading = false
                            }).catch(e => {
                                this.model1 = false;
                                this.query();
                                this.buttonLoading = false
                                this.$Message.error("数据获取失败")
                            })
                        } else {
                            this.form1datacopy.updateUserId = userId;
                            this.form1datacopy.updateOrgId = orgId;
                            this.$http.post(Api.updateFireSupplier, this.form1datacopy).then((res) => {
                                this.$Message.success("保存成功")
                                this.model1 = false;
                                this.query();
                                this.buttonLoading = false
                            }).catch(e => {
                                this.model1 = false;
                                this.query();
                                this.buttonLoading = false
                                this.$Message.error("数据获取失败")
                            })
                        }
                    } else {
                        this.$Message.error('请填报完整!');
                    }
                })
            },
            addInfo() {
                this.form1datacopy = {};
                this.add = true;
                this.model1 = true;
            },
            formatDate(date, fmt) {
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                let o = {
                    'M+': date.getMonth() + 1,
                    'd+': date.getDate(),
                    'h+': date.getHours(),
                    'm+': date.getMinutes(),
                    's+': date.getSeconds()
                };
                for (let k in o) {
                    if (new RegExp(`(${k})`).test(fmt)) {
                        let str = o[k] + '';
                        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
                    }
                }
                return fmt;
            },
            padLeftZero(str) {
                return ('00' + str).substr(str.length);
            },
        },
    }
</script>
<style lang='css' scoped>
    @import '../../../../index.css';

</style>
